
package com.gzbsoft.sc.modules.sys.service.impl;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.gzbsoft.sc.common.utils.R;
import com.gzbsoft.sc.modules.app.form.RegisterForm;
import com.gzbsoft.sc.modules.sys.dao.JxUserDao;
import com.gzbsoft.sc.modules.sys.dao.SysMenuDao;
import com.gzbsoft.sc.modules.sys.dao.SysUserDao;
import com.gzbsoft.sc.modules.sys.entity.JxUser;
import com.gzbsoft.sc.modules.sys.entity.SysUser;
import com.gzbsoft.sc.modules.sys.service.SysUserService;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;
@Service
public class SysUserServiceImpl extends BaseServiceImpl<SysUser, Long> implements SysUserService {
    private final SysUserDao sysUserDao;
    private final SysMenuDao sysMenuDao;
    private final JxUserDao jxUserDao;
    public SysUserServiceImpl(SysUserDao sysUserDao, SysMenuDao sysMenuDao , JxUserDao jxUserDao) {
        this.sysUserDao = sysUserDao;
        this.sysMenuDao = sysMenuDao;
        this.jxUserDao = jxUserDao;
    }
    @Override
    public List<Long> queryAllMenuId(Long userId) {
        return sysMenuDao.queryAllMenuId(userId);
    }
    @Override
    public Optional<SysUser> queryByUserName(String username) {
        return sysUserDao.findByProperty("username", username);
    }
    @Override
    @Transactional
    public SysUser save(SysUser user) {
        //sha256加密
        String salt = RandomUtil.randomString(20);
        user.setPassword(new Sha256Hash(user.getPassword(), salt).toHex());
        user.setSalt(salt);
        return sysUserDao.save(user);
    }
    @Override
    @Transactional
    public SysUser update(SysUser user) {
        if (StrUtil.isBlank(user.getPassword())) {
            user.setPassword(null);
        } else {
            user.setPassword(new Sha256Hash(user.getPassword(), user.getSalt()).toHex());
        }
        return super.update(user);
    }
    @Override
    @Transactional
    public SysUser selectUserInfo(String username ,String id){
        sysUserDao.updatetUserInfo(username,id);
        return  null;
    }

    @Override
    @Transactional
    public int addUserInfo(String username , String passowrd){
        JxUser user = new JxUser();
        user.setUsername(username);
        user.setPassword(passowrd);
        jxUserDao.save(user);
        return 1;
    }

    @Override
    @Transactional
    public void register(RegisterForm form){
        JxUser user = new JxUser();
        user.setTel(form.getMobile());
        user.setUsername(form.getMobile());
        user.setPassword(DigestUtil.sha256Hex(form.getPassword()));
        user.setUserType(form.getUserType());
        jxUserDao.save(user);
    }
    @Override
    @Transactional
    public R isRegisterByTel(String mobile){
        Optional<JxUser> jxUser = jxUserDao.findByProperty("tel",mobile);
        if(StringUtils.isEmpty(jxUser) ){
            return R.ok();
        }
        return R.error(201,"当前手机号已注册");
    }
}
